home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 187_01 / copyfile.c < prev    next >
C/C++ Source or Header  |  1985-12-28  |  2KB  |  51 lines

  1. /*@*****************************************************/
  2. /*@                                                    */
  3. /*@ copyfile -  file named <ifn> to file named <ofn>.  */
  4. /*@     if a file named <ofn> exists, it is deleted    */ 
  5. /*@     prior to copy.                                 */
  6. /*@                                                    */
  7. /*@   Usage:     copyfile(infilename, outfilename);    */
  8. /*@       where infilename is ASCIIZ input filename.   */
  9. /*@            outfilename is ASCIIZ output filename.  */
  10. /*@                                                    */
  11. /*@       returns zero if successful, 1 or 2 otherwise.*/
  12. /*@       1 => input open failed.                      */
  13. /*@       2 => output open failed.                     */
  14. /*@       NOTE: opens and closes files.                */
  15. /*@                                                    */
  16. /*@*****************************************************/
  17.  
  18.  
  19. char cpy_buf[1024];
  20.  
  21. int copyfile(ifn, ofn)
  22. char *ifn, *ofn;
  23. {
  24.     int ifp, ofp, ret, retv;
  25.  
  26.     retv = 0;
  27.  
  28.     if (0 == (ifp = fopen(ifn, "r")))
  29.         retv |= 1;            /* cannot open input file */
  30.  
  31.     if (!retv)
  32.         if (0 == (ofp = fopen(ofn, "w")))
  33.             retv |= 2;            /* cannot open output file */
  34.  
  35.  
  36.     if (!retv) {
  37.         ret = 1;
  38.         while (ret > 0) 
  39.             if ((ret = read(ifp, cpy_buf, 1024)) > 0)
  40.                 ret = write(ofp, cpy_buf, ret);
  41.     }
  42.  
  43.     if (!(retv & 1))        /* if ifp open */
  44.         fclose(ifp);
  45.     if (!(retv & 2))        /* if ofp open */
  46.         fclose(ofp);
  47.  
  48.     return retv;
  49.  
  50. }
  51.